package com.atguigu.product.controller;

import java.util.Arrays;
import java.util.Map;

import com.atguigu.common.valid.updateStatus;
import com.atguigu.product.entity.BrandEntity;
import com.atguigu.common.valid.AddValid;
import com.atguigu.common.valid.updateValid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.atguigu.product.service.BrandService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;


/**
 * 品牌
 *
 * @author litong
 * @email 2943284183@qq.com
 * @date 2024-08-07 18:11:07
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = brandService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    public R info(@PathVariable("brandId") Long brandId){
		BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@Validated(AddValid.class) @RequestBody BrandEntity brand/*,BindingResult result*/){
//        if(result.hasErrors()){
//            Map<String,String> map=new HashMap<>();
//            result.getFieldErrors().forEach(item->{
////                获取错误提示
//                String message = item.getDefaultMessage();
////                获取错误属性的名字
//                String field = item.getField();
//                map.put(field,message);
//            });
//            return R.error().put("data",map);
//        }else {
            brandService.save(brand);
            return R.ok();
//        }

    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@Validated(updateValid.class)@RequestBody BrandEntity brand){
//        更新品牌表的时候要修改商品表等多个表
		brandService.updateDetail(brand);

        return R.ok();
    }

    /**
     * 只修改状态
     */
    @RequestMapping("/update/status")
    public R updateStatus(@Validated(updateStatus.class)@RequestBody BrandEntity brand){
        brandService.updateById(brand);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] brandIds){
		brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

}
